//
//  SelectAddTimeVC.m
//  Doctor
//
//  Created by 阳光 on 16/10/10.
//  Copyright © 2016年 阳光. All rights reserved.
//  选择时间

#import "SelectAddTimeVC.h"
#import "TimeListModel.h"


@interface SelectAddTimeVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,strong)UIView *tipView;

// 已经选中的时间数组
@property(nonatomic,strong)NSMutableArray *selectTimeFromTimeListArr;

@property(nonatomic,strong)NSMutableArray *selectOrderTimeArr;

@end

@implementation SelectAddTimeVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _selectOrderTimeArr=[NSMutableArray array];
    
    // title上个界面传过来的
    self.title=_timeStr;
    
    [self initNavBarBtnItem];
    
   
    
    // 第一种方式:获取当前已经被预定过的时间数据     从上个界面传过来
    _selectTimeFromTimeListArr=[NSMutableArray array];
    for (int i=0;i<_selectTimeArr.count;i++)
    {
        NSString *orderTimeBtnStr=[NSString stringWithFormat:@"%@--%@",[[[Util getConvertToFormalDateByTimeStamp:_selectTimeArr[i][@"start_time"]] componentsSeparatedByString:@" "] lastObject],[[[Util getConvertToFormalDateByTimeStamp:_selectTimeArr[i][@"end_time"]] componentsSeparatedByString:@" "] lastObject]];
        
        [_selectTimeFromTimeListArr addObject:orderTimeBtnStr];
    }
    
    NSLog(@"_selectTimeFromTimeListArr--%@",_selectTimeFromTimeListArr);
    
     [self initSrollView];
     // 第二种方式:获取当前已经被预定过的时间数据    直接请求服务器数据
    [self getLoadData];
    
}

// 提交或者修改点击触发事件
-(void)OKBtnEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

 // 第二种方式:获取当前已经被预定过的时间数据    直接请求服务器数据
-(void)getLoadData
{
    // 开始时间
    NSString *startDayStr=[NSString stringWithFormat:@"%@ %@",_timeStr,@"00:00"];
    // 结束时间
    NSString *endDayStr=[NSString stringWithFormat:@"%@ %@",_timeStr,@"23:59"];
    
    NSLog(@"3000023---%@  sdddd--%@",startDayStr,endDayStr);
    
    // 当医生所在医院只有一个时,直接获取
    if (![[Util getUserDefaultObjectForKey:@"ShopId"] isEqualToString:@"0"])
    {
        _hospitalID=[Util getUserDefaultObjectForKey:@"ShopId"];
    }
    
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"shop_id":_hospitalID,
                        @"start_tag":[Util getTimeStampByDate:startDayStr],
                        @"end_tag":[Util getTimeStampByDate:endDayStr]
                        };
    NSLog(@"002923932211ii---%@",dic);
    
    [QBRequest OrderTimeListWithDic:dic Success:^(id res)
     {
         NSLog(@"获取当天已经被预定的时间－－－%@",res);
         _selectOrderTimeArr=[TimeListModel mj_objectArrayWithKeyValuesArray:res[@"info"]];
         NSLog(@"902092990092390932-=--%@  --%lu",_selectOrderTimeArr,(unsigned long)_selectOrderTimeArr.count);
         
         NSMutableArray *tt=[NSMutableArray array];
         for (NSDictionary *dic in res[@"info"])
         {
             NSString *orderTimeBtnStr=[NSString stringWithFormat:@"%@--%@",[[[Util getConvertToFormalDateByTimeStamp:dic[@"start_time"]] componentsSeparatedByString:@" "] lastObject],[[[Util getConvertToFormalDateByTimeStamp:dic[@"end_time"]] componentsSeparatedByString:@" "] lastObject]];
             
             [tt addObject:orderTimeBtnStr];
         }
         NSLog(@"we0e9ew99ew-%@",tt);
         
         
         [self initBottomView];
         
     } failure:^(NSString *error)
    {
         
     }];
    
}


-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(kmargin,kmargin,APPW-kmargin*2,0)];
    mainView.backgroundColor=WhiteColor;
    
    // 顶线
    UIView *topLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW-kmargin*2,1)];
    topLine.backgroundColor=Main_Color;
    [mainView addSubview:topLine];
    setViewHeight_Add(mainView,1);
    
    UILabel *tipLabel=[mainView getLabelWithText:NSLocalizedString(@"select time", nil) font:Font_Number(13) textColor:Color666666 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];//@"请选择时间"
    //    tipLabel.frame=CGRectMake(kmargin,40,APPW/2,30);
    tipLabel.frame=CGRectMake(kmargin,mainView.frame.size.height,APPW/2,30);
    [mainView addSubview:tipLabel];
    
    setViewHeight_Add(mainView,kmargin*3);
    
    UIView *amView=[[UIView alloc]initWithFrame:CGRectMake(kmargin,CGRectGetMaxY(tipLabel.frame),APPW-kmargin*4,kmargin*7)];
    amView.tag=900;
    amView.backgroundColor=ColorFAFAFA;
    ViewBorderRadius(amView,kmargin/5,.5,ColorBDBDBD);
    [mainView addSubview:amView];
    
    UILabel *label=[amView getLabelWithText:@"上\n午" font:Font_Number(15) textColor:Main_Color textAlignment:NSTextAlignmentCenter lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:0];
    [amView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin/2);
        make.centerY.mas_equalTo(amView.mas_centerY);
    }];
    
    NSArray *amTimeArr=@[@"09:00--09:30",@"09:30--10:00",@"10:00--10:30",@"10:30--11:00",@"11:00--11:30",@"11:30--12:00"];
    
    if ([_selectTimeFromTimeListArr isEqualToArray:amTimeArr])
    {
        // 两个数组完全一样的
    }
    else
    {
        NSLog(@"w8e9weueuwe--%@",_selectTimeFromTimeListArr);
        
        
        NSArray *amArr=[amTimeArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@",_selectTimeFromTimeListArr]];
        NSLog(@"上午＊＊＊两个数组只有一些相同的数据为----%@",amArr);
    }
    
    
#define amTimeW  (APPW-kmargin*4-30)/2
#define amTimeH  kmargin*7/3
    for (int i=0;i<amTimeArr.count;i++)
    {
        UIButton *timeBtn=[[UIButton alloc]initWithFrame:CGRectMake(30+(i%2)*amTimeW,(i/2)*amTimeH,amTimeW-.5,amTimeH-.5)];
        ViewBorderRadius(timeBtn,1,.5,ColorBDBDBD);
        [amView addSubview:timeBtn];
        timeBtn.tag=i+1;
        timeBtn.titleLabel.font=Font_Number(14);
        [timeBtn setTitle:amTimeArr[i] forState:0];
        [timeBtn setTitleColor:ColorBDBDBD forState:0];
        [timeBtn addTarget:self action:@selector(timeBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        
        if ([timeBtn.titleLabel.text isEqualToString:amTimeArr[i]])
        {
            
        }
        
    }
    
    for (NSInteger i=1;i<8;i++)
    {
        UIButton *amBtn=(UIButton *)[amView viewWithTag:i];
        
        [_selectTimeFromTimeListArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
         {
             if ([obj isEqualToString:amBtn.titleLabel.text])
             {
                 amBtn.backgroundColor=Main_Color;
                [amBtn setTitleColor:WhiteColor forState:0];
                 amBtn.selected=YES;
             }
        }];
    }
   
    setViewHeight_Add(mainView,kmargin*7+kmargin*2);
    
    UIView *pmView=[[UIView alloc]initWithFrame:CGRectMake(kmargin,CGRectGetMaxY(amView.frame)+kmargin*2,APPW-kmargin*4,kmargin*14)];
    pmView.backgroundColor=ColorFAFAFA;
    ViewBorderRadius(pmView,kmargin/5,.5,ColorBDBDBD);
    [mainView addSubview:pmView];
    
    UILabel *pmLabel=[amView getLabelWithText:@"下\n午" font:Font_Number(15) textColor:Main_Color textAlignment:NSTextAlignmentCenter lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:0];
    [pmView addSubview:pmLabel];
    [pmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin/2);
        make.centerY.mas_equalTo(pmView.mas_centerY);
    }];
    
    NSArray *pmTimeArr=@[@"12:00--12:30",@"12:30--13:00",@"13:00--13:30",@"13:30--14:00",@"14:00-- 14:30",@"14:30--15:00",@"15:00--15:30",@"15:30--16:00",@"16:00--16:30",@"16:30--17:00",@"17:00--17:30",@"17:30--18:00"];
    
    if ([_selectTimeFromTimeListArr isEqualToArray:pmTimeArr])
    {
        // 两个数组完全一样的
    }
    else
    {
        NSArray *pmArr=[pmTimeArr filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF in %@",_selectTimeFromTimeListArr]];
        NSLog(@"下午＊＊＊两个数组只有一些相同的数据为----%@",pmArr);
    }
    
    
#define pmTimeW  (APPW-kmargin*4-30)/2
#define pmTimeH  kmargin*7/3
    
    for (int i=0;i<pmTimeArr.count;i++)
    {
        UIButton *timeBtn=[[UIButton alloc]initWithFrame:CGRectMake(30+(i%2)*pmTimeW,(i/2)*pmTimeH,pmTimeW-.5,pmTimeH-.5)];
        ViewBorderRadius(timeBtn,1,.5,ColorBDBDBD);
        [pmView addSubview:timeBtn];
        timeBtn.tag=i+7;
        timeBtn.titleLabel.font=Font_Number(14);
        [timeBtn setTitle:pmTimeArr[i] forState:0];
        [timeBtn setTitleColor:ColorBDBDBD forState:0];
        [timeBtn addTarget:self action:@selector(timeBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    for (NSInteger i=7;i<19;i++)
    {
        UIButton *pmBtn=(UIButton *)[pmView viewWithTag:i];
        
        [_selectTimeFromTimeListArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
         {
             if ([obj isEqualToString:pmBtn.titleLabel.text])
             {
                 pmBtn.backgroundColor=Main_Color;
                 [pmBtn setTitleColor:WhiteColor forState:0];
                 pmBtn.selected=YES;
             }
         }];
    }
    setViewHeight_Add(mainView,kmargin*14+kmargin);
    return mainView;
}


-(void)timeBtnEvent:(UIButton *)sender
{
    NSArray *selectTimeArr=[sender.currentTitle componentsSeparatedByString:@"--"];

    NSLog(@"-00w0ew09e-%@",sender.currentTitle);
    
    NSString *firstTimeStr=[NSString stringWithFormat:@"%@ %@",_timeStr,selectTimeArr[0]];
    NSString *secondTimeStr=[NSString stringWithFormat:@"%@ %@",_timeStr,[selectTimeArr lastObject]];
    NSDictionary *dic=@{
                        @"doctor_id":[Util getUserDefaultObjectForKey:@"id"],
                        @"shop_id":_hospitalID,
                        @"start_time":[Util getTimeStampByFormalDate:firstTimeStr],
                        @"end_time":[Util getTimeStampByFormalDate:secondTimeStr]
                        };
    [QBRequest AddOrderTimeWithDic:dic Success:^(id res)
     {
         NSLog(@"-30033--%@ --%@  ---%@",res,dic,res[@"message"]);
         
     } failure:^(NSString *error)
     {
         
     }];
    
    sender.selected=!sender.selected;
    if (sender.selected)
    {
        sender.backgroundColor=Main_Color;
        [sender setTitleColor:WhiteColor forState:0];
    }
    else
    {
        sender.backgroundColor=ClearColor;
        [sender  setTitleColor:ColorBDBDBD forState:0];
    }
}


-(void)initBottomView
{
    UIButton *OKBtn=[UIButton new];
    [self.view addSubview:OKBtn];
    [OKBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kmargin);
        make.size.mas_equalTo(CGSizeMake(APPW-kmargin*2,40));
        make.bottom.mas_equalTo(-kmargin*2);
    }];
    OKBtn.backgroundColor=Main_Color;
    OKBtn.titleLabel.font=Font_Number(16);
    ViewBorderRadius(OKBtn,kmargin/2,1,ClearColor);
    
    // 之前没有预定过时间时,刚进来显示为提交,否则为修改
    if (_selectOrderTimeArr.count>0)
    {
        [OKBtn setTitle:@"修改" forState:0];
    }
    else
    {
        [OKBtn setTitle:@"提交" forState:0];
    }
    [OKBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [OKBtn addTarget:self action:@selector(OKBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)initSrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=MAIN_BACK_COLOR;
    _scrollView.delegate=self;
    _scrollView.scrollEnabled=YES;
    _scrollView.userInteractionEnabled=YES;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,kmargin);
    [_scrollView addSubview:_mainView];
    
    _tipView=[self tipView];
    setViewCoord_Y(_tipView,_mainView.frame.size.height+_mainView.frame.origin.y+kmargin);
    [_scrollView addSubview:_tipView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.tipView.frame)+kmargin);
    
}

-(UIView *)tipView
{
    UIView *tipView=[[UIView alloc]initWithFrame:CGRectMake(kmargin,0,APPW-kmargin*2,0)];
    NSString *tipStr=NSLocalizedString(@"AddVideoDescription",nil);//@"说明:视频预约时间只可更新次日起至一个礼拜,已提交的时间段可做修改,但如果是被预约的时间段则不可做任何修改,请按时参加视频初诊。时间为诊所当地时间,展示在用户版是换算成中国时间。";
    CGSize contentSize=[tipStr boundingRectWithSize:CGSizeMake(APPW-kmargin*4,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:Font_Number(13) }context:nil].size;
    UILabel *tipLabel=[tipView getLabelWithText:tipStr font:Font_Number(13) textColor:Color757575 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:0];
    tipLabel.frame=CGRectMake(kmargin,tipView.frame.size.height,APPW-kmargin*4,contentSize.height);
    [tipView addSubview:tipLabel];
    
    setViewHeight_Add(tipView,contentSize.height);
    
    return tipView;
}


-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
